* Model 1: Hays model
* Original model maximum likelihood

clear
pr drop _all
set matsize 800
set more off

***********************
*  Likelihood Evaluator                                                           
***********************

program define splag_ll

args lnf mu rho sigma
tempvar A rSL
gen `A'= ones - `rho'*EIGS1
gen `rSL'=`rho'*SL1
qui replace `lnf'= ln(`A') + ln(normalden($ML_y1-`rSL'-`mu', 0, `sigma'))
end

global nobs = 581

***********************
* Open Data For Weights
***********************
clear
use "tax_w.dta", clear
***********************
mkmat var1-var$nobs, matrix(W)
matrix eigenvalues eig1 imaginaryv = W
matrix eig2 = eig1'
matrix ones=J($nobs,1,1)


**************************
* Open Data for Regression
**************************
drop _all
use "Tax_Analysis_CH4.dta", clear
**************************
global Y tax
global X tax_1 capital_1 undens1 leftc1 eu1 cap2_end cap2_cons cap2_udens cap2_left cap2_eu c1-c19 y8-y41 
mkmat $Y, matrix(Y)
matrix SL = W*Y
svmat SL, n(SL)
svmat eig2, n(EIGS)
svmat ones, n(ones)

*************************
*Produce starting values 
*************************
qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

***************************
*Estimate spatial lag model
***************************

	ml model lf splag_ll (mu: $Y=$X) (rho:) (sigma:), technique(dfp) 
	ml init OLSb
	ml init rho:_cons=0
	ml init sigma:_cons=`OLSsigma'
	*ml check
	*ml report
	ml max, difficult ltolerance(1e-8) tolerance(1e-8)

predict yhat
mkmat yhat, matrix(yhat)
matrix e2 = (Y-yhat)'*(Y-yhat) 
scalar e2 = e2[1,1]
qui summarize $Y
matrix ym = r(mean)*J($nobs,1,1)        
matrix ym2 = (Y-ym)'*(Y-ym)
scalar ym2 = ym2[1,1] 
scalar r2 = 1 - (e2/ym2)
scalar list r2


* Model 3: LDV excluded
* Original model maximum likelihood

clear
pr drop _all
set matsize 800
set more off

***********************
*  Likelihood Evaluator                                                           
***********************

program define splag_ll

args lnf mu rho sigma
tempvar A rSL
gen `A'= ones - `rho'*EIGS1
gen `rSL'=`rho'*SL1
qui replace `lnf'= ln(`A') + ln(normalden($ML_y1-`rSL'-`mu', 0, `sigma'))
end

global nobs = 581

***********************
* Open Data For Weights
***********************
clear
use "tax_w.dta", clear
***********************
mkmat var1-var$nobs, matrix(W)
matrix eigenvalues eig1 imaginaryv = W
matrix eig2 = eig1'
matrix ones=J($nobs,1,1)


**************************
* Open Data for Regression
**************************
drop _all
use "Tax_Analysis_CH4.dta", clear
**************************
global Y tax
global X  capital_1 undens1 leftc1 eu1 cap2_end cap2_cons cap2_udens cap2_left cap2_eu c1-c19 y8-y41 
mkmat $Y, matrix(Y)
matrix SL = W*Y
svmat SL, n(SL)
svmat eig2, n(EIGS)
svmat ones, n(ones)

*************************
*Produce starting values 
*************************
qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

***************************
*Estimate spatial lag model
***************************

	ml model lf splag_ll (mu: $Y=$X) (rho:) (sigma:), technique(dfp) 
	ml init OLSb
	ml init rho:_cons=0
	ml init sigma:_cons=`OLSsigma'
	*ml check
	*ml report
	ml max, difficult ltolerance(1e-8) tolerance(1e-8)

predict yhat
mkmat yhat, matrix(yhat)
matrix e2 = (Y-yhat)'*(Y-yhat) 
scalar e2 = e2[1,1]
qui summarize $Y
matrix ym = r(mean)*J($nobs,1,1)        
matrix ym2 = (Y-ym)'*(Y-ym)
scalar ym2 = ym2[1,1] 
scalar r2 = 1 - (e2/ym2)
scalar list r2


* Model 4: No period FE
* Original model maximum likelihood

clear
pr drop _all
set matsize 800
set more off

***********************
*  Likelihood Evaluator                                                           
***********************

program define splag_ll

args lnf mu rho sigma
tempvar A rSL
gen `A'= ones - `rho'*EIGS1
gen `rSL'=`rho'*SL1
qui replace `lnf'= ln(`A') + ln(normalden($ML_y1-`rSL'-`mu', 0, `sigma'))
end

global nobs = 581

***********************
* Open Data For Weights
***********************
clear
use "tax_w.dta", clear
***********************
mkmat var1-var$nobs, matrix(W)
matrix eigenvalues eig1 imaginaryv = W
matrix eig2 = eig1'
matrix ones=J($nobs,1,1)


**************************
* Open Data for Regression
**************************
drop _all
use "Tax_Analysis_CH4.dta", clear
**************************
global Y tax
global X tax_1 capital_1 undens1 leftc1 eu1 cap2_end cap2_cons cap2_udens cap2_left cap2_eu c1-c19  
mkmat $Y, matrix(Y)
matrix SL = W*Y
svmat SL, n(SL)
svmat eig2, n(EIGS)
svmat ones, n(ones)

*************************
*Produce starting values 
*************************
qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

***************************
*Estimate spatial lag model
***************************

	ml model lf splag_ll (mu: $Y=$X) (rho:) (sigma:), technique(dfp) 
	ml init OLSb
	ml init rho:_cons=0
	ml init sigma:_cons=`OLSsigma'
	*ml check
	*ml report
	ml max, difficult ltolerance(1e-8) tolerance(1e-8)

predict yhat
mkmat yhat, matrix(yhat)
matrix e2 = (Y-yhat)'*(Y-yhat) 
scalar e2 = e2[1,1]
qui summarize $Y
matrix ym = r(mean)*J($nobs,1,1)        
matrix ym2 = (Y-ym)'*(Y-ym)
scalar ym2 = ym2[1,1] 
scalar r2 = 1 - (e2/ym2)
scalar list r2


* Model 5: No LDV and no period FE
* Original model maximum likelihood

clear
pr drop _all
set matsize 800
set more off

***********************
*  Likelihood Evaluator                                                           
***********************

program define splag_ll

args lnf mu rho sigma
tempvar A rSL
gen `A'= ones - `rho'*EIGS1
gen `rSL'=`rho'*SL1
qui replace `lnf'= ln(`A') + ln(normalden($ML_y1-`rSL'-`mu', 0, `sigma'))
end

global nobs = 581

***********************
* Open Data For Weights
***********************
clear
use "tax_w.dta", clear
***********************
mkmat var1-var$nobs, matrix(W)
matrix eigenvalues eig1 imaginaryv = W
matrix eig2 = eig1'
matrix ones=J($nobs,1,1)


**************************
* Open Data for Regression
**************************
drop _all
use "Tax_Analysis_CH4.dta", clear
**************************
global Y tax
global X capital_1 undens1 leftc1 eu1 cap2_end cap2_cons cap2_udens cap2_left cap2_eu c1-c19 
mkmat $Y, matrix(Y)
matrix SL = W*Y
svmat SL, n(SL)
svmat eig2, n(EIGS)
svmat ones, n(ones)

*************************
*Produce starting values 
*************************
qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

***************************
*Estimate spatial lag model
***************************

	ml model lf splag_ll (mu: $Y=$X) (rho:) (sigma:), technique(dfp) 
	ml init OLSb
	ml init rho:_cons=0
	ml init sigma:_cons=`OLSsigma'
	*ml check
	*ml report
	ml max, difficult ltolerance(1e-8) tolerance(1e-8)

predict yhat
mkmat yhat, matrix(yhat)
matrix e2 = (Y-yhat)'*(Y-yhat) 
scalar e2 = e2[1,1]
qui summarize $Y
matrix ym = r(mean)*J($nobs,1,1)        
matrix ym2 = (Y-ym)'*(Y-ym)
scalar ym2 = ym2[1,1] 
scalar r2 = 1 - (e2/ym2)
scalar list r2

* Model 6: No country FE
* Original model maximum likelihood

clear
pr drop _all
set matsize 800
set more off

***********************
*  Likelihood Evaluator                                                           
***********************

program define splag_ll

args lnf mu rho sigma
tempvar A rSL
gen `A'= ones - `rho'*EIGS1
gen `rSL'=`rho'*SL1
qui replace `lnf'= ln(`A') + ln(normalden($ML_y1-`rSL'-`mu', 0, `sigma'))
end

global nobs = 581

***********************
* Open Data For Weights
***********************
clear
use "tax_w.dta", clear
***********************
mkmat var1-var$nobs, matrix(W)
matrix eigenvalues eig1 imaginaryv = W
matrix eig2 = eig1'
matrix ones=J($nobs,1,1)


**************************
* Open Data for Regression
**************************
drop _all
use "Tax_Analysis_CH4.dta", clear
**************************
global Y tax
global X tax_1 capital_1 undens1 leftc1 eu1 cap2_end cap2_cons cap2_udens cap2_left cap2_eu y8-y41 
mkmat $Y, matrix(Y)
matrix SL = W*Y
svmat SL, n(SL)
svmat eig2, n(EIGS)
svmat ones, n(ones)

*************************
*Produce starting values 
*************************
qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

***************************
*Estimate spatial lag model
***************************

	ml model lf splag_ll (mu: $Y=$X) (rho:) (sigma:), technique(dfp) 
	ml init OLSb
	ml init rho:_cons=0
	ml init sigma:_cons=`OLSsigma'
	*ml check
	*ml report
	ml max, difficult ltolerance(1e-8) tolerance(1e-8)

predict yhat
mkmat yhat, matrix(yhat)
matrix e2 = (Y-yhat)'*(Y-yhat) 
scalar e2 = e2[1,1]
qui summarize $Y
matrix ym = r(mean)*J($nobs,1,1)        
matrix ym2 = (Y-ym)'*(Y-ym)
scalar ym2 = ym2[1,1] 
scalar r2 = 1 - (e2/ym2)
scalar list r2


* Model 7: contiguity not row-standardized
* Original model maximum likelihood

clear
pr drop _all
set matsize 800
set more off

***********************
*  Likelihood Evaluator                                                           
***********************

program define splag_ll

args lnf mu rho sigma
tempvar A rSL
gen `A'= ones - `rho'*EIGS1
gen `rSL'=`rho'*SL1
qui replace `lnf'= ln(`A') + ln(normalden($ML_y1-`rSL'-`mu', 0, `sigma'))
end

global nobs = 581

***********************
* Open Data For Weights
***********************
clear
use "tax_w_nonrowstandardized.dta", clear
***********************
mkmat var1-var$nobs, matrix(W)
matrix eigenvalues eig1 imaginaryv = W
matrix eig2 = eig1'
matrix ones=J($nobs,1,1)


**************************
* Open Data for Regression
**************************
drop _all
use "Tax_Analysis_CH4.dta", clear
**************************
global Y tax
global X tax_1 capital_1 undens1 leftc1 eu1 cap2_end cap2_cons cap2_udens cap2_left cap2_eu c1-c19 y8-y41 
mkmat $Y, matrix(Y)
matrix SL = W*Y
svmat SL, n(SL)
svmat eig2, n(EIGS)
svmat ones, n(ones)

*************************
*Produce starting values 
*************************
qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

***************************
*Estimate spatial lag model
***************************

	ml model lf splag_ll (mu: $Y=$X) (rho:) (sigma:), technique(dfp) 
	ml init OLSb
	ml init rho:_cons=0
	ml init sigma:_cons=`OLSsigma'
	*ml check
	*ml report
	ml max, difficult ltolerance(1e-8) tolerance(1e-8)

predict yhat
mkmat yhat, matrix(yhat)
matrix e2 = (Y-yhat)'*(Y-yhat) 
scalar e2 = e2[1,1]
qui summarize $Y
matrix ym = r(mean)*J($nobs,1,1)        
matrix ym2 = (Y-ym)'*(Y-ym)
scalar ym2 = ym2[1,1] 
scalar r2 = 1 - (e2/ym2)
scalar list r2


* Model 8: 1/distance
* Original model maximum likelihood

clear
pr drop _all
set matsize 800
set more off

***********************
*  Likelihood Evaluator                                                           
***********************

program define splag_ll

args lnf mu rho sigma
tempvar A rSL
gen `A'= ones - `rho'*EIGS1
gen `rSL'=`rho'*SL1
qui replace `lnf'= ln(`A') + ln(normalden($ML_y1-`rSL'-`mu', 0, `sigma'))
end

global nobs = 581

***********************
* Open Data For Weights
***********************
clear
use "tax_w_distinv.dta", clear
***********************
mkmat var1-var$nobs, matrix(W)
matrix eigenvalues eig1 imaginaryv = W
matrix eig2 = eig1'
matrix ones=J($nobs,1,1)


**************************
* Open Data for Regression
**************************
drop _all
use "Tax_Analysis_CH4.dta", clear
**************************
global Y tax
global X tax_1 capital_1 undens1 leftc1 eu1 cap2_end cap2_cons cap2_udens cap2_left cap2_eu c1-c19 y8-y41 
mkmat $Y, matrix(Y)
matrix SL = W*Y
svmat SL, n(SL)
svmat eig2, n(EIGS)
svmat ones, n(ones)

*************************
*Produce starting values 
*************************
qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

***************************
*Estimate spatial lag model
***************************

	ml model lf splag_ll (mu: $Y=$X) (rho:) (sigma:), technique(dfp) 
	ml init OLSb
	ml init rho:_cons=0
	ml init sigma:_cons=`OLSsigma'
	*ml check
	*ml report
	ml max, difficult ltolerance(1e-8) tolerance(1e-8)

predict yhat
mkmat yhat, matrix(yhat)
matrix e2 = (Y-yhat)'*(Y-yhat) 
scalar e2 = e2[1,1]
qui summarize $Y
matrix ym = r(mean)*J($nobs,1,1)        
matrix ym2 = (Y-ym)'*(Y-ym)
scalar ym2 = ym2[1,1] 
scalar r2 = 1 - (e2/ym2)
scalar list r2


* Model 9: 1/ln(distance)
* Original model maximum likelihood
clear
pr drop _all
set matsize 800
set more off

***********************
*  Likelihood Evaluator                                                           
***********************

program define splag_ll

args lnf mu rho sigma
tempvar A rSL
gen `A'= ones - `rho'*EIGS1
gen `rSL'=`rho'*SL1
qui replace `lnf'= ln(`A') + ln(normalden($ML_y1-`rSL'-`mu', 0, `sigma'))
end

global nobs = 581

***********************
* Open Data For Weights
***********************
clear
use "tax_w_lndistinv.dta", clear
***********************
mkmat var1-var$nobs, matrix(W)
matrix eigenvalues eig1 imaginaryv = W
matrix eig2 = eig1'
matrix ones=J($nobs,1,1)


**************************
* Open Data for Regression
**************************
drop _all
use "Tax_Analysis_CH4.dta", clear
**************************
global Y tax
global X tax_1 capital_1 undens1 leftc1 eu1 cap2_end cap2_cons cap2_udens cap2_left cap2_eu c1-c19 y8-y41 
mkmat $Y, matrix(Y)
matrix SL = W*Y
svmat SL, n(SL)
svmat eig2, n(EIGS)
svmat ones, n(ones)

*************************
*Produce starting values 
*************************
qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

***************************
*Estimate spatial lag model
***************************

	ml model lf splag_ll (mu: $Y=$X) (rho:) (sigma:), technique(dfp) 
	ml init OLSb
	ml init rho:_cons=0
	ml init sigma:_cons=`OLSsigma'
	*ml check
	*ml report
	ml max, difficult ltolerance(1e-8) tolerance(1e-8)

predict yhat
mkmat yhat, matrix(yhat)
matrix e2 = (Y-yhat)'*(Y-yhat) 
scalar e2 = e2[1,1]
qui summarize $Y
matrix ym = r(mean)*J($nobs,1,1)        
matrix ym2 = (Y-ym)'*(Y-ym)
scalar ym2 = ym2[1,1] 
scalar r2 = 1 - (e2/ym2)
scalar list r2


* Model 10: 1/reversed distance
* Original model maximum likelihood
clear
pr drop _all
set matsize 800
set more off

***********************
*  Likelihood Evaluator                                                           
***********************

program define splag_ll

args lnf mu rho sigma
tempvar A rSL
gen `A'= ones - `rho'*EIGS1
gen `rSL'=`rho'*SL1
qui replace `lnf'= ln(`A') + ln(normalden($ML_y1-`rSL'-`mu', 0, `sigma'))
end

global nobs = 581

***********************
* Open Data For Weights
***********************
clear
use "tax_w_distrevinv.dta", clear
***********************
mkmat var1-var$nobs, matrix(W)
matrix eigenvalues eig1 imaginaryv = W
matrix eig2 = eig1'
matrix ones=J($nobs,1,1)


**************************
* Open Data for Regression
**************************
drop _all
use "Tax_Analysis_CH4.dta", clear
**************************
global Y tax
global X tax_1 capital_1 undens1 leftc1 eu1 cap2_end cap2_cons cap2_udens cap2_left cap2_eu c1-c19 y8-y41 
mkmat $Y, matrix(Y)
matrix SL = W*Y
svmat SL, n(SL)
svmat eig2, n(EIGS)
svmat ones, n(ones)

*************************
*Produce starting values 
*************************
qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

***************************
*Estimate spatial lag model
***************************

	ml model lf splag_ll (mu: $Y=$X) (rho:) (sigma:), technique(dfp) 
	ml init OLSb
	ml init rho:_cons=0
	ml init sigma:_cons=`OLSsigma'
	*ml check
	*ml report
	ml max, difficult ltolerance(1e-8) tolerance(1e-8)

predict yhat
mkmat yhat, matrix(yhat)
matrix e2 = (Y-yhat)'*(Y-yhat) 
scalar e2 = e2[1,1]
qui summarize $Y
matrix ym = r(mean)*J($nobs,1,1)        
matrix ym2 = (Y-ym)'*(Y-ym)
scalar ym2 = ym2[1,1] 
scalar r2 = 1 - (e2/ym2)
scalar list r2


